package registration.adventurezone.thevine.com;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import registration.adventurezone.thevine.com.Constants.ChildrenFilter;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class CheckInActivity extends Activity{

    private ListView childrenListView;
    private Button inButton;
    EditText filterBox;
    private ArrayList<ChildViewHolder> childrenList;
    private ChildViewHolder[] childViews;
    private ArrayAdapter<ChildViewHolder> listAdapter;
    File childrenDatabaseFile;



	@Override
    public void onCreate( Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkin_layout);

        childrenListView = (ListView) findViewById(R.id.childrenList);
        childrenListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View item, int position,
					long id) {
				ChildViewHolder child = listAdapter.getItem(position);
				child.toggleChecked();
				//TODO these next two lines are a mess...at least I got rid of the weird Child and ChildViewHolder abstractions
				ChildViewHolder viewHolder = (ChildViewHolder) item.getTag();
				viewHolder.getCheckBox().setChecked(child.isChecked());
				
			}
		});
    	
        ArrayList<DBChild> children = XMLResourceHandler.getInstance().getChildrenByStatus(ChildrenFilter.CHECKED_OUT);
    	childViews = new ChildViewHolder[children.size()];
    	for (int i=0;i<children.size();i++) {
    		childViews[i] = new ChildViewHolder(children.get(i));
    	}

    	childrenList = new ArrayList<ChildViewHolder>();
        childrenList.addAll(Arrays.asList(childViews));
        listAdapter = new ChildArrayAdapter(this, childrenList, R.layout.simplerow);
        childrenListView.setAdapter(listAdapter);
        

        filterBox = (EditText) findViewById(R.id.childrenFilter);


        // By using setTextFilterEnabled method in listview we can filter the listview items.

        childrenListView.setTextFilterEnabled(true);
        
        filterBox.addTextChangedListener(new TextWatcher()
        {


            @Override
            public void onTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
            {
                // TODO Auto-generated method stub

            }



            @Override
            public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
            {
                // TODO Auto-generated method stub

            }



            @Override
            public void afterTextChanged( Editable arg0)
            {
                CheckInActivity.this.listAdapter.getFilter().filter(arg0);
            }
        });
        
        inButton = (Button) findViewById(R.id.inButton);
        inButton.setOnClickListener(new OnClickListener() {           
        	public void onClick(View v) {
        		for(ChildViewHolder c : childrenList) {
        			if(c.isChecked()) {
        				ArrayList<DBChild> children = new ArrayList<DBChild>();
        				children.add(c.getDBChild());
        				XMLResourceHandler.getInstance().checkInSelectedChildren(children);
        				
        			}
        		}
        		XMLResourceHandler.getInstance().writeXMLToMem(XMLResourceHandler.getInstance().writeDocToXMLString());
        		Intent myIntent = new Intent(CheckInActivity.this, AZregActivity.class);
        		CheckInActivity.this.startActivity(myIntent);
        	}
		});
        
    }
    
}
